<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="操作类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goldActionType">
              <j-dict-select-tag type="list" v-model="model.stoneActionType" dictCode="stone_action_type" placeholder="请选择操作类型" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="采购单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseOrderNum">
              <a-input v-model="model.purchaseOrderNum" placeholder="请输入采购单号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="石号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneNum">
              <a-input v-model="model.stoneNum" placeholder="请输入石号" readOnly ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="石号名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneName">
              <a-input v-model="model.stoneName" placeholder="请输入石号名称" readOnly ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="石卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="codeNum">
              <a-input v-model="model.codeNum" placeholder="请输入石卡号" readOnly ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="石类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cateId">
              <j-search-select-tag v-model="model.cateId" dict="stone_category,cate_name,id" placeholder="请选择石类" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="筛号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sizeNum">
              <a-input v-model="model.sizeNum" placeholder="请输入筛号" readOnly ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag type="list" v-model="model.level" dictCode="level" placeholder="请选择级别" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="形状" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shape">
              <j-dict-select-tag type="list" v-model="model.shape" dictCode="shape" placeholder="请选择形状" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="style">
              <j-dict-select-tag type="list" v-model="model.style" dictCode="style" placeholder="请选择规格" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="净度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="neatness">
              <j-dict-select-tag type="list" v-model="model.neatness" dictCode="stone_clarity" placeholder="请选择净度" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="车工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cutting">
              <j-dict-select-tag type="list" v-model="model.cutting" dictCode="stone_clarity" placeholder="请选择车工" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="color">
              <j-dict-select-tag type="list" v-model="model.color" dictCode="color" placeholder="请选择颜色" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="质地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="texture">
              <j-dict-select-tag type="list" v-model="model.texture" dictCode="color" placeholder="请选择质地" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="角度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="angle">
              <a-input v-model="model.angle" placeholder="请输入角度"  readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="成本单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneUnitCost">
              <a-input v-model="model.stoneUnitCost" placeholder="请输入成本单价"  readOnly style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneUnitPrice">
              <a-input v-model="model.stoneUnitPrice" placeholder="请输入单价" readOnly style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单粒重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
              <a-input v-model="model.weight" placeholder="请输入单粒重" style="width: 100%" readOnly />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="好石数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodStoneCount">
              <a-input-number v-model="model.goodStoneCount" placeholder="请输入好石数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="好石重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodStoneWeight">
              <a-input v-model="model.goodStoneWeight" placeholder="请输入好石重" readOnly style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="坏石数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="badStoneCount">
              <a-input-number v-model="model.badStoneCount" placeholder="请输入坏石数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="坏石重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="badStoneWeight">
              <a-input-number v-model="model.badStoneWeight" placeholder="请输入坏石重" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="镶法" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inlay">
              <j-dict-select-tag type="list" v-model="model.inlay" dictCode="inlay_type" placeholder="请选择镶法" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="批号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchNum">
              <a-input v-model="model.batchNum" placeholder="请输入批号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="来单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveOrderNum">
              <a-input v-model="model.receiveOrderNum" placeholder="请输入来单号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="主副石" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainStone">
              <j-dict-select-tag type="list" v-model="model.mainStone" dictCode="main_stone_type" placeholder="请选择主副石" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceType">
              <j-dict-select-tag type="list" v-model="model.invoiceType" dictCode="invoice_type" placeholder="请选择发票类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实际币种" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currencyType">
              <j-dict-select-tag type="list" v-model="model.currencyType" dictCode="currency_type" placeholder="请选择实际币种" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实际单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneUnitPlay">
              <a-input v-model="model.stoneUnitPlay" placeholder="请输入实际单价"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实际金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneCost">
              <a-input v-model="model.stoneCost" placeholder="请输入实际金额"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="汇率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="exchangeRate">
              <a-input v-model="model.exchangeRate" placeholder="请输入汇率"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"  ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'StoneEntryForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            count:0,
            pureStoneWeight:0,
            unitPrice:0,
            costAmount:0,
            payAmount:0,
            goldContent:0,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           karat: [
              { required: true, message: '请输入成色!'},
           ],
           withStoneWeight: [
              { required: true, message: '请输入连石重!'},
           ],
        },
        url: {
          add: "/org.jeecg.modules.goldentry/goldEntry/add",
          edit: "/org.jeecg.modules.goldentry/goldEntry/edit",
          queryById: "/org.jeecg.modules.goldentry/goldEntry/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch : {
      immediate:true,
      'model.goodStoneCount' : function() {
        if(this.model.weight != null && this.model.weight !='' && this.model.goodStoneCount != null && this.model.goodStoneCount !='') {
          this.model.goodStoneWeight = this.model.weight * this.model.goodStoneCount
        } else {
          this.model.goodStoneWeight = ''
        }
      },
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>